<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<%@ taglib uri="/WEB-INF/tld/sys.tld" prefix="sys" %>
<%
    String path = request.getContextPath();
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>${tabTitle}</title>
    <jsp:include page="../../import.jsp" flush="true"/>
</head>
<body class="iframeBody">
<div class="Wrapper">
    <div>
        <div id="pageInTab" class="pageInTab">
            <form id="ins_cfg_info_edit_form">
                <div style="text-align: center">
                    <br/>
                    <span style="color: #3c85ad"><b>身份证信息</b></span>
                    <br/>
                </div>
                <div class="fieldset1 tabel_col clearFix">
                    <div class="fieldBox col_50">
                        <div class="label">姓</div>
                        <div class="field">
                            <input name="cstFnm" id="cstFnm" type="text" class="ui-textbox" value="${cstFnm}" onchange="convertName();" validate="{required:true}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_50">
                        <div class="label">名</div>
                        <div class="field">
                            <input name="cstLnm" id="cstLnm" type="text" class="ui-textbox" value="${cstLnm}" onchange="convertName();" validate="{required:true}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_100">
                        <div class="label">拼音</div>
                        <div class="field">
                            <span id="cstPyNm">${cstPyFNm}${cstPyLNm}
                                <%--<input name="cstPyNm" id="cstPyNm" type="text" class="ui-textbox" value="${cstPyFNm}${cstPyLNm}" disabled="disabled">--%>
                            </span>
                        </div>
                    </div>
                    <div class="fieldBox col_100">
                        <div class="label">身份证签发机关</div>
                        <div class="field">
                            <input name="issuingOrg" id="issuingOrg" type="text" class="ui-textbox" value="${issuingOrg}" validate="{required:true}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_33">
                        <div class="label">身份证地址</div>
                        <div class="field">
                            <input name="postAddr" id="postAddr" type="text" class="ui-textbox" value="${postAddr}" validate="{required:true}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_50">
                        <div class="label">现居住地</div>
                        <div class="field">
                            <jsp:include page="../../system/sysRegion/region.jsp" flush="true">
                                <jsp:param name="regionShowLvl"  value="3"/>
                            </jsp:include>
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_100">
                        <span style="color: red"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;提示：身份证地址需要是身份证实际地址，并且需要是农村地址</b></span>
                    </div>
                    <div class="fieldBox col_100" style="text-align: center">
                        <br/>
                        <span style="color: #3c85ad"><b>资产信息</b></span>
                        <br/>
                    </div>
                    <div class="fieldBox col_33">
                        <div class="label">银行卡号</div>
                        <div class="field">
                            <input name="accNo" id="accNo" type="text" class="ui-textbox" value="${accNo}"
                                   validate="{number:true}">(任意银行)
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_33">
                        <div class="label">银行预留手机号</div>
                        <div class="field">
                            <input name="mp" id="mp" type="text" class="ui-textbox" value="${mp}" validate="{number:true}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_33">
                        <div class="label">个人月收入(元)</div>
                        <div class="field">
                            <span>${monthInc}</span>
                        </div>
                    </div>
                    <div class="fieldBox col_33">
                        <div class="label">个人总资产(万)</div>
                        <div class="field">
                            <input name="tolPsonlAst" id="tolPsonlAst" type="text" class="ui-textbox" value="${tolPsonlAst}" validate="{number:true}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>

                    <div class="fieldBox col_33">
                        <div class="label">住宅入住日期</div>
                        <div class="field">
                            <input name="rsidOcpyDt" id="rsidOcpyDt" type="text" class="ui-datepicker" value="${rsidOcpyDt}" validate="{required:true}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_33">
                        <div class="label">累计负债余额（万）</div>
                        <div class="field">
                            <input name="othDebTbal" id="othDebTbal" type="text" class="ui-textbox" value="${othDebTbal}" validate="{number:true}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_33">
                        <div class="label">累计负债月还（元）</div>
                        <div class="field">
                            <input name="monRepayAmt" id="monRepayAmt" type="text" class="ui-textbox" value="${monRepayAmt}" validate="{number:true}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>

                    <div class="fieldBox col_100" style="text-align: center">
                        <span style="color: #3c85ad"><b>担保人/紧急联系人</b></span>
                    </div>
                    <div class="fieldBox col_100_auto">
                        <div id="ligergrid_icbcyt" class="sq-grid"></div>
                    </div>

                    <!-- hide data -->
                    <div class="fieldBox col_100" style="display: none;">
                        <div class="field">
                            <input name="cstPyFNm" id="cstPyFNm" type="text" class="ui-textbox" value="${cstPyFNm}">
                            <input name="cstPyLNm" id="cstPyLNm" type="text" class="ui-textbox" value="${cstPyLNm}">
                            <input name="insCode" id="insCode" type="text" class="ui-textbox" value="${insCode}">
                            <input name="fundRasInsNm" id="fundRasInsNm" type="text" class="ui-textbox" value="${fundRasInsNm}">
                            <input name="monthInc" id="monthInc" type="text" class="ui-textbox" value="${monthInc}">
                            <input name="prjId" id="prjId" type="text" class="ui-textbox" value="${prjId}">
                            <input name="cstId" id="cstId" type="text" class="ui-textbox" value="${cstId}">
                            <input name="cstNm" id="cstNm" type="text" class="ui-textbox" value="${cstNm}">
                            <input name="cstAccInfoId" id="cstAccInfoId" type="text" class="ui-textbox" value="${cstAccInfoId}">
                            <input name="fundRasInsId" id=fundRasInsId type="text" class="ui-textbox" value="${fundRasInsId}">
                             <input name="bkCd" id="bkCd" type="text" class="ui-textbox" value="${bkCd}">
                             <input name="bkNm" id="bkNm" type="text" class="ui-textbox" value="${bkNm}">
                            <input name="certNo" id=certNo type="text" class="ui-textbox" value="${certNo}">
                            <input name="certTypCd" id=certTypCd type="text" class="ui-textbox" value="${certTypCd}">
                        </div>
                    </div>
                </div>
                <div id="saveInsCfgDiv" class="btnsBox" style="text-align: center">
                    <span id="saveInsCfg" class="sq-button sq-button-round" onclick="return saveInfo();">保存</span>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="text/javascript">
    var ligergrid_icbcyt;
    var detailFlag = '${detailFlag}';
    var updateFlag = '${updateFlag}';
    var fromCAFlag = '${fromCAFlag}';
    var fromCMFlag = '${fromCMFlag}';

    $(function(){
        var prjId='${prjId}';
        var cstId='${cstId}';

        if(null != detailFlag && "true" == detailFlag) {
            initGrid(prjId, cstId, false);
        }else{
            initGrid(prjId, cstId, true);
        }
        initData();
        disabledInput();
        enableFields();
    });
    /**
     * 初始化担保人、紧急联系人
     *
     **/
    function initGrid(prjId, cstId,flag){
        ligergrid_icbcyt =  $("#ligergrid_icbcyt").ligerGrid({
            enabledEdit: flag,//是否能编辑
            method: 'get',
            url: '<%=path%>/institution/icbcZbOrYt/initList.do?prjId='+prjId+'&cstId='+cstId,
            columns: [
                { display: '姓名', name: 'lnkNm', minWidth: 50,align:'center',enabledSort:false},
                { display: '手机号', name: 'mp', minWidth: 50,align:'center',enabledSort:false},
                { display: '身份证到期日',
                    columns:[
                        { display: '日期', name: 'certValidEDt', maxWidth: 200 ,align:'center',render: renderDate,editor: { type: 'date' }},
                        {display: '是否长期',name: 'certValidCheckEDt',maxWidth: 15 ,align:'left',render: renderCheckDate}
                    ], maxWidth: 215
                }


            ],
            //onSuccess:successGird,
            pageSize: 10,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            heightDiff: -2,
            checkbox: false,
            frozen: false,
            allowUnSelectRow: true,
            alternatingRow: false,
            rownumbers:true//列表是否有行号
        });
    }
    /**
     * 初始化
     */
    function initData() {

        $("#ins_cfg_info_edit_form").ligerForm({
            validate: true
        });
        //区域 省、市、县
        setRgnProvCdUpdataValue('${postProvince}');
        setRgnCityCdUpdataValue('${postCity}');
        setRgnCountyCdUpdataValue('${postCounty}');
    }
    /**
     * 查看详情页面置灰 不可编辑
     **/
    function disabledInput() {

        if(detailFlag == "true") {

            liger.get("cstFnm").setDisabled();
            liger.get("cstLnm").setDisabled();
            liger.get("issuingOrg").setDisabled();
            liger.get("postAddr").setDisabled();
            liger.get("rgnProvCd").setDisabled();
            liger.get("rgnCityCd").setDisabled();
            liger.get("rgnCountyCd").setDisabled();
            liger.get("accNo").setDisabled();
            liger.get("mp").setDisabled();
            liger.get("tolPsonlAst").setDisabled();
            liger.get("rsidOcpyDt").setDisabled();
            liger.get("othDebTbal").setDisabled();
            liger.get("monRepayAmt").setDisabled();
            $("#saveInsCfgDiv").hide();
        }
    }

    /**
     * 是否展示保存按钮
     **/
    function enableFields() {

        if (null != fromCAFlag && "true" == fromCAFlag) {

            $("#saveInsCfgDiv").show();
        }
        if (null != fromCMFlag && "true" == fromCMFlag) {

            $("#saveInsCfgDiv").show();
        }
    }


    /**
     * 日期
     * @param rowdata
     * @param index
     * @param value
     * @returns {string}
     */
    function renderDate(rowdata,index,value){
        var dateStr = "";
        if(null != value && value != '' && '长期' != value ){
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
        }
        return dateStr;
    }
    // function renderDate(rowdata,index,value){
    //     var certValidCheckEDt= "#certValidCheckEDt"+rowdata.id;
    //     console.log("renderId:"+certValidCheckEDt);
    //     var dateStr = "";
    //     var certCheckEDt= $(certValidCheckEDt).val();
    //     if(null != value && '长期' != value ){
    //         var date = new Date(value);
    //         dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
    //         $(certValidCheckEDt).attr("checked",false);
    //     }else if (value == '长期'){
    //         $(certValidCheckEDt).attr("checked",true);
    //         dateStr='';
    //     }
    //     return dateStr;
    // }
    /**
     *自定义复选框
     */
    function renderCheckDate(rowdata,index,value){
        var id = "certValidCheckEDt"+rowdata.id;

        if ( null != detailFlag && "true" == detailFlag){
            if ("长期"==rowdata.certValidEDt){
                return '<input type="checkbox" name="certValidCheckEDt" disabled="disabled" id="'+id+'" checked value="长期"/>&nbsp;长期';
            }else{
                return '<input type="checkbox" name="certValidCheckEDt" disabled="disabled" id="'+id+'" value="长期"/>&nbsp;长期';
            }
        }else{
            if ("长期"==rowdata.certValidEDt){
                return '<input type="checkbox" name="certValidCheckEDt"  id="'+id+'" checked value="长期"/>&nbsp;长期';
            }else{
                return '<input type="checkbox" name="certValidCheckEDt"  id="'+id+'" value="长期"/>&nbsp;长期';
            }
        }
    }
    /**
     *  汉字转换拼音 (将姓和名转换为拼音并进行大写)
     */
    function convertName(){
        var chinaFirstName = $('#cstFnm').val();
        var chinaLastName = $('#cstLnm').val();
        //获取拼音全拼大写（调用js中方法）
        var pinYinFirstName = pinyin.getUpperFullChars(chinaFirstName);
        var pinYinLastName = pinyin.getUpperFullChars(chinaLastName);
        //给两个文本框赋值
        $('#cstPyFNm').val(pinYinFirstName);
        $('#cstPyLNm').val(pinYinLastName);
        $('#cstPyNm').html(pinYinFirstName+pinYinLastName);
    }

    /**
     * 保存表单数据
     */
    function saveInfo(){
        var ligerForm = liger.get("ins_cfg_info_edit_form");
        if (!ligerForm.valid()) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
            //验证不通过，不执行以下代码
            return false;
        }
        var data = ligerForm.getData();
        //身份证信息
        if (!$("#cstFnm").val()) {
            top.$.ligerDialog.SQalert({content: "姓为空，请维护!", SQ_type: "error"});
            return false;
        }
        if (!$("#cstLnm").val()) {
            top.$.ligerDialog.SQalert({content: "名为空，请维护!", SQ_type: "error"});
            return false;
        }
        if (!$("#issuingOrg").val()) {
            top.$.ligerDialog.SQalert({content: "身份证签发机关为空，请维护!", SQ_type: "error"});
            return false;
        }
        if (!$("#postAddr").val()) {
            top.$.ligerDialog.SQalert({content: "身份证地址为空，请维护!", SQ_type: "error"});
            return false;
        }
        var rgnProvCd = getRgnProvCd();
        var rgnCityCd = getRgnCityCd();
        var rgnCountyCd = getRgnCountyCd();
        if(!rgnProvCd || ' ' == rgnProvCd){
            top.$.ligerDialog.SQalert({content: "现居住地-省为空，请维护!", SQ_type: "error"});
            return false;
        }
        if(!rgnCityCd || ' ' == rgnCityCd){
            top.$.ligerDialog.SQalert({content: "现居住地-市为空，请维护!", SQ_type: "error"});
            return false;
        }
        if(!rgnCountyCd || ' ' == rgnCountyCd){
            top.$.ligerDialog.SQalert({content: "现居住地-县为空，请维护!", SQ_type: "error"});
            return false;
        }

        //资产信息
        if (!$("#accNo").val()) {
            top.$.ligerDialog.SQalert({content: "银行卡号为空，请维护!", SQ_type: "error"});
            return false;
        }
        if (!$("#mp").val()) {
            top.$.ligerDialog.SQalert({content: "预留手机号为空，请维护!", SQ_type: "error"});
            return false;
        }


        if (!$("#tolPsonlAst").val()) {
            top.$.ligerDialog.SQalert({content: "个人总资产为空，请维护!", SQ_type: "error"});
            return false;
        }

        if (!$("#rsidOcpyDt").val()) {
            top.$.ligerDialog.SQalert({content: "住宅入住日期为空，请维护!", SQ_type: "error"});
            return false;
        }

        if (!$("#othDebTbal").val()) {
            top.$.ligerDialog.SQalert({content: "累计负债余额为空，请维护!", SQ_type: "error"});
            return false;
        }
        if (!$("#monRepayAmt").val()) {
            top.$.ligerDialog.SQalert({content: "累计负债月还为空，请维护!", SQ_type: "error"});
            return false;
        }

        //获取table数据 并根据是否勾选长期
        var icbctyGrid = $("#ligergrid_icbcyt").ligerGrid("getData");
        if(icbctyGrid.length < 1 ){
            top.$.ligerDialog.SQalert({content: "请维护担保人/紧急联系人信息!", SQ_type: "error"});
            return false;
        }
        for (var i=0; i <icbctyGrid.length;i++){
            var id=icbctyGrid[i].id;
            var certValidCheckEDtValue = $("#certValidCheckEDt"+id).is(':checked');
            if (certValidCheckEDtValue){
                icbctyGrid[i].certValidEDt ='长期';
            }

            if(null == icbctyGrid[i].certValidEDt || '' == icbctyGrid[i].certValidEDt || (!certValidCheckEDtValue && '长期' == icbctyGrid[i].certValidEDt)){
                top.$.ligerDialog.SQalert({content: "担保人/紧急联系人身份证到期日为空，请维护", SQ_type: "error"});
                return false;
            }

            var certValidEDate = new Date(icbctyGrid[i].certValidEDt);
            var currentDate = new Date(DateUtils.formatDate(new Date(), DateUtils.YYYY_MM_DD));//当前日期
            if(certValidEDate.getTime() < currentDate.getTime()){
                top.$.ligerDialog.SQalert({content: "身份证到期日不能小于当前日期!", SQ_type: "error"});
                return false;
            }
        }

        //系统信息
        data["id"] = $("#cstAccInfoId").val();//客户账户信息ID
        data["cstAccInfoId"] = $("#cstAccInfoId").val();//客户账户信息ID
        data["prjId"] = $("#prjId").val();//项目ID
        data["cstId"] = $("#cstId").val();//客户ID
        data["cstNm"] = $("#cstNm").val();//客户名称
        // data["fundRasInsId"] = $("#fundRasInsId").val();//机构名称
        data["insId"] = $("#fundRasInsId").val();//机构名称
        data["insCode"] = $("#insCode").val();//机构code

        //身份证信息
        data["certTypCd"] = $("#certTypCd").val();//证件类型
        data["certNo"] = $("#certNo").val();//证件号码
        data["cstFnm"] = $("#cstFnm").val();//姓
        data["cstLnm"] = $("#cstLnm").val();//名
        data["cstPyFnm"] = $("#cstPyFNm").val();//拼音姓
        data["cstPyLnm"] = $("#cstPyLNm").val();//拼音名
        data["lssueOffice"] = $("#issuingOrg").val();//身份证签发机关
        data["postAddr"] = $("#postAddr").val();//身份证地址
        data["postProvince"] = getRgnProvCd();//现居住地省
        data["postCity"] = getRgnCityCd();//现居住地市
        data["postCounty"] = getRgnCountyCd();//现居住地县
        //资产信息
        data["accNo"] = $("#accNo").val();//银行账号
        data["mp"] = $("#mp").val();//银行预留手机号
        data["monthInc"] = $("#monthInc").val();//月收入(元)
        data["tolPsonlAst"] = $("#tolPsonlAst").val();//个人总资产(万)
        data["rsidOcpyDt"] = $("#rsidOcpyDt").val();//住宅入住日期
        data["othDebTbal"] = $("#othDebTbal").val();//累计负债余额（万）
        data["monRepayAmt"] = $("#monRepayAmt").val();//累计负债月还（元）
        data["linkManList"] = JSON.stringify(icbctyGrid);//紧急联系人与担保人

        var url = '<%=path%>/institution/icbcZbOrYt/saveForIcbcZbOrYt.do';
        var wait_dialog;
        $.ajax({
            type:"post",
            url: url,
            data: data,
            dataType:"json",
            async:false,
            cache:false,
            beforeSend: function () {
                wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
            },
            success: function (msg) {
                wait_dialog.close();
                if (msg.success == 1) {
                    //新增时赋值
                    if ($("#cstAccInfoId").val() == "" && msg.data != null && msg.data.prjCstId != null) {
                        $("#cstAccInfoId").val(msg.data.prjCstId);
                    }
                    UIUtils.alertSuccessHold1(msg.info);
                } else {
                    UIUtils.alertErrorHold1(msg.info);
                }
            }
        });
    }
</script>
</body>
</html>